package ar.com.personalTravellers.rowProcessors;

import ar.com.personalTravellers.beans.Neighborhood;
import ar.com.personalTravellers.beans.Zone;

import com.angel.common.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowDataException;
import com.angel.io.parameters.ParametersService;
import com.angel.io.processors.commands.imports.ImportRowProcessorCommand;
import com.angel.io.type.rows.ImportRowFile;
import com.angel.io.type.rows.impl.HeaderRowFile;

/**
 *
 * @author William
 */
public class NeighborhoodRowProcessorCommand implements ImportRowProcessorCommand {

    public static final String NAME_COLUMN = "name";
    public static final String ZONE_COLUMN = "zone";
    public static final String ACTIVE_COLUMN = "active";

    public void checkRowData(ImportRowFile importRowFile, HeaderRowFile headerRowFile) throws InvalidRowDataException {
        String name = importRowFile.getStringValue(headerRowFile.getColumnPosition(NAME_COLUMN));
        String zone = importRowFile.getStringValue(headerRowFile.getColumnPosition(ZONE_COLUMN));
        String active = importRowFile.getStringValue(headerRowFile.getColumnPosition(ACTIVE_COLUMN));

        boolean allAreNotEmpty = StringHelper.areAllNotEmpty(name, zone, active);

        if(!allAreNotEmpty && Zone.valueOf(zone.toUpperCase()) == null && Boolean.valueOf(active) == null){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"Name: [" + name + "] - " +
                    "Active: [" + active + "] - " +
                    "Zone: [" + zone + "]." 
                    );
		}
    }

    public Object processRow(ImportRowFile importRowFile, HeaderRowFile headerRowFile, ParametersService parametersService) {
        String name = importRowFile.getStringValue(headerRowFile.getColumnPosition(NAME_COLUMN));
        String active = importRowFile.getStringValue(headerRowFile.getColumnPosition(ACTIVE_COLUMN));
        String zone = importRowFile.getStringValue(headerRowFile.getColumnPosition(ZONE_COLUMN)).toUpperCase();

        Boolean isActive = Boolean.valueOf(active);

        Neighborhood neighborhood = new Neighborhood();
        neighborhood.setName(name);
        neighborhood.setZone(Zone.valueOf(zone));
        neighborhood.setActive(isActive);
        return neighborhood;
    }



    public String[] getHeaderColumnsNames() {
        return new String[]{
                NAME_COLUMN,
                ZONE_COLUMN,
                ACTIVE_COLUMN
        };
    }
}